package com.wiegand.gui.navigation {
	import com.asual.swfaddress.SWFAddress;
	import com.asual.swfaddress.SWFAddressEvent;
	import com.caurina.transitions.Tweener;
	import com.wiegand.event.ClickEvent;
	import com.wiegand.gui.IGuiElement;
	import com.wiegand.gui.button.NavButton;

	import flash.display.Sprite;

	/**
	 * @author gandy
	 */
	public class MainNavigation extends Sprite implements IGuiElement {
		private var _buttons : Array;
		private var _xml : XMLList;
		private var _mouseFunction : Function;
				
		public static const EVENT_CHANGE : String = "change content";

		public function MainNavigation(xml : XMLList, mouseFunction:Function) {
			_xml = xml;
			_mouseFunction = mouseFunction;
		}
		
		public function init(): void{
			_buttons = new Array();
			
			var foo_y : int = 0;
			for( var i:int=0; i<_xml.children().length(); i++ ){
				var item : NavButton = new NavButton(_xml.child(i), _mouseFunction);
				item.index=i;
				item.y = foo_y;
				item.x = -300;
				foo_y += item.height + int(_xml.@vSpace);
				addChild(item);
				_buttons.push({bt:item,y:item.y,index:i});
			}
			x = int(_xml.@x);
			y = int(_xml.@y);	
			
			SWFAddress.addEventListener(SWFAddressEvent.CHANGE, swfAdressChange);
		}
		
		private function swfAdressChange(e:SWFAddressEvent):void {
			for(var i:int=0;i<_buttons.length;i++){
				if( String(e.pathNames[0]) == String(NavButton(_buttons[i].bt).name) ){ 
					NavButton(_buttons[i].bt).setActive(false);
				}
				else NavButton(_buttons[i].bt).setActive(true);
			}
		}		

		private function addListeners() : void {
			for( var i:int=0; i<_buttons.length; i++ ){
				NavButton(_buttons[i].bt).addEventListener(_xml.child(i).@event, click);
			}
		}
			
		private function removeListeners() : void {
			for( var i:int=0; i<_buttons.length; i++ ){
				NavButton(_buttons[i].bt).addEventListener(_xml.child(i).@event, click);
			}
		}

		private function click(e:ClickEvent) : void {
			for(var i:int=0;i<_buttons.length;i++){
				if( e.currentTarget == NavButton(_buttons[i].bt) ){ 
					NavButton(_buttons[i].bt).setActive(false);
					SWFAddress.setValue( e.value );
				}
				else{ 
					NavButton(_buttons[i].bt).setActive(true);
				}
			}
		}
		
		public function open(delay : Number = 0) : void {
			for( var i:int=0; i<_buttons.length; i++ ){
				var item : NavButton = _buttons[i].bt;
				if(i!=_buttons.length-1) Tweener.addTween(item,{x:0,delay:delay+(i*.2),time:1,transition:"easeoutback"});
				else Tweener.addTween(item,{x:0,delay:delay+(i*.2),time:1,transition:"easeoutback",onComplete:addListeners});
			}					
		}
		
		public function close(delay : Number = 0) : void {
			removeListeners();
			for( var i:int=0; i<_buttons.length; i++ ){
				var item : NavButton = _buttons[i].bt;
				Tweener.addTween(item,{x:-300,delay:delay+(i*.1),time:.5,transition:"easeinback"});
			}			
		}
		
		public function show(delay : Number = 0) : void {
		}
		
		public function hide(delay : Number = 0) : void {
		}
		
		public function destroy() : void {
			removeListeners();
		}
		
		public function resizeHandler() : void {
		}	
	}
}
